using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BouncinBall.Components
{
    public class VisibleComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private Texture2D _texture;
        private string _textureString;
        public float Depth { get; set; }

        private Vector2 _screenPosition;
        private float _spriteScale;

        public void SetSpriteScale(float widthInMeters)
        {
            int widthInPixels = (int) (((BouncinBall.BouncingBallGame) Game).PixelsPerMeter * widthInMeters);
            _spriteScale = widthInPixels / (float) _texture.Width;
        }

        //public void SetPosition(Utility.Position pos)
        //{
        //    SetPosition(pos.LateralPosition);
        //}

        //public void SetPosition(Vector2 pos)
        //{
        //    SetPosition(pos.X, pos.Y);
        //}

        //public void SetPosition(float x, float y)
        //{
        //    _screenPosition = ((BouncinBall.BouncingBallGame)Game).Field.GetScreenPosition(x, y);
        //}

        public void SetScreenPosition(Utility.Position posComp, Utility.Position posPaddle)
        {              
            _screenPosition = (posComp.LateralPosition - posPaddle.LateralPosition) //position offset in meters
                * (int) ((BouncinBall.BouncingBallGame) Game).PixelsPerMeter //convert to pixels
                //+ new Vector2(((BouncinBall.BouncingBallGame) Game).ScreenWidthPixels / 2.0F, ((BouncinBall.BouncingBallGame) Game).ScreenHeightPixels / 2.0F) //center image on screen
                + new Vector2(480 / 2.0F, 632 / 2.0F) //center image on paddle
                - new Vector2(_texture.Width * _spriteScale / 2.0F, _texture.Height * _spriteScale / 2.0F); //center image on point
        }

        public VisibleComponent(Game game, string textureFilename, float depth)
            : base(game)
        {
            _textureString = textureFilename;
            Depth = depth;
        }

        protected override void LoadContent()
        {
            base.LoadContent();

            _texture = this.Game.Content.Load<Texture2D>(_textureString);
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            base.Draw(gameTime);

            ((BouncingBallGame)Game).SpriteBatch.Draw(_texture, _screenPosition, null, Color.White, 0, Vector2.Zero, _spriteScale, SpriteEffects.None, Depth);
        }
    }
}
